package cn.shadow.game.chess.CHnchess.chessman;


import cn.shadow.game.chess.CHnchess.board.Board;
import cn.shadow.game.chess.CHnchess.board.Line;
import cn.shadow.game.chess.CHnchess.board.Position;
import cn.shadow.game.chess.CHnchess.type.Camp;
import cn.shadow.game.chess.CHnchess.type.ChessType;


/**
 * 将/帅
 */
public class Commander extends AbstractChess implements Line {

    public Commander(Board board, Camp camp) {
        super(ChessType.SHUAI, board, camp);
    }


    @Override
    public void addLeftPoint() {
        int row = getRow();
        int col = getCol();
        Position point = getBoard().getPoint(row, col - 1);
        if (point != null) {
            addPoint(point);
        }
    }

    private void addPoint(Position point) {
        if (point.isOverRanged()) {
            return;
        }
        if (!point.isInCenter()) {
            return;
        }

        //如果的竖直坐标上有对方的帅，就返回,特殊处理了
        for (int i = point.getRow() + 1; i < Board.ROW; i++) {
            Position position = getBoard().getPoint(i, point.getCol());
            Chess chess = position.getChess();
            if (chess != null && chess != this) {
                if (chess.getType() == ChessType.SHUAI) {
                    return;
                }
                break;
            }
        }
        for (int i = point.getRow() - 1; i >= 0; i--) {
            Position position = getBoard().getPoint(i, point.getCol());
            Chess chess = position.getChess();
            if (chess != null && chess != this) {
                if (chess.getType() == ChessType.SHUAI) {
                    return;
                }
                break;
            }
        }
        Chess chess = point.getChess();
        if (chess != null) {
            if (chess.getCamp() == this.getCamp()) {
                return;
            }
        }
        canSetPosition.add(point);
        canAttackPosition.add(point);
    }

    @Override
    public void addRightPoint() {
        int row = this.getRow();
        int col = this.getCol();
        Position point = getBoard().getPoint(row, col + 1);
        if (point != null) {
            addPoint(point);
        }
    }

    @Override
    public void addUpPoint() {
        int row =getRow();
        int col =getCol();
        Position point = getBoard().getPoint(row - 1, col);
        if (point != null) {
            addPoint(point);
        }
    }

    @Override
    public void addDownPoint() {
        int row =getRow();
        int col =getCol();
        Position point = getBoard().getPoint(row + 1, col);
        if (point != null) {
            addPoint(point);
        }
    }

    @Override
    public boolean betweenCommanders() {
        return false;
    }

}
